#import "CTAlertView.h"
@interface CTAlertView ()
@property (nonatomic ,strong) UIView *coverView;
@property (nonatomic ,strong) UIImageView *bgImageView;
@property (nonatomic ,strong) UILabel *tipLabel;
@property (nonatomic ,strong) UILabel *contentLabel;
@property (nonatomic ,strong) UIButton *fistBtn;
@property (nonatomic ,strong) UIButton *secondBtn;
@property (nonatomic ,strong) UIView *closeCoverView;
@property (nonatomic ,assign) CGFloat alertHeight;
@end
@implementation CTAlertView
+ (instancetype)alertWithAttributedTitle:(NSAttributedString *)title 
                      attributedSubTitle:(NSAttributedString *)subTitle
                                firstBtn:(NSAttributedString *)firBtnTitle
                               secondBtn:(NSAttributedString *)secondBtn
                                   click:(void(^)(NSString *title))click
{
    CTAlertView *alertView = [[CTAlertView alloc] init];
    alertView.tipLabel.attributedText = title;
    alertView.contentLabel.attributedText = subTitle;
    return alertView;
}
- (instancetype)init
{
    if (self = [super init]) {
        self.frame = UIScreen.mainScreen.bounds;
        _alertHeight = Ratio(270);
        [self setSubViews];
        [self setConstraints];
        self.coverView = [[UIView alloc] initWithFrame:UIScreen.mainScreen.bounds];
        self.coverView.backgroundColor = UIColorFromRGBA(0x00000, 0.36);
        self.bgImageView.ct_size = CGSizeMake(270, 150);
        self.bgImageView.center = self.center;
    }
    return self;
}
- (void)setSubViews
{
    [self addSubview:self.coverView];
    [self addSubview:self.bgImageView];
    [self.bgImageView addSubview:self.tipLabel];
    [self.bgImageView addSubview:self.contentLabel];
    [self.bgImageView addSubview:self.secondBtn];
    [self.bgImageView addSubview:self.fistBtn];
    [self.bgImageView addSubview:self.closeCoverView];
}
- (void)setConstraints
{
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(285);
        make.height.mas_equalTo(341);
        make.centerX.equalTo(self.coverView);
        make.centerY.equalTo(self.coverView);
    }];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(27);
        make.top.mas_equalTo(26);
        make.height.mas_equalTo(22);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tipLabel);
        make.top.equalTo(self.tipLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(17);
    }];
    [self.fistBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-17);
        make.left.mas_equalTo(18);
        make.right.mas_equalTo(-18);
    }];
    [self.secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(20);
        make.width.height.mas_equalTo(12);
    }];
    [self.closeCoverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.secondBtn);
        make.width.height.mas_equalTo(30);
    }];
}
#pragma mark - lazyLoading
- (UIView *)coverView
{
    if (_coverView == nil) {
        _coverView = [[UIView alloc] init];
        _coverView.alpha = 0.5;
        _coverView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [_coverView addGestureRecognizer:tap];
    }
    return _coverView;
}
- (UIImageView *)bgImageView
{
    if (_bgImageView == nil) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.backgroundColor = [UIColor whiteColor];
        _bgImageView.userInteractionEnabled = YES;
    }
    return _bgImageView;
}
- (UILabel *)tipLabel
{
    if (_tipLabel == nil) {
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.textColor = [UIColor whiteColor];
        _tipLabel.font = kFontSize(22);
        _tipLabel.text = @"";
    }
    return _tipLabel;
}
- (UILabel *)contentLabel
{
    if (_contentLabel == nil) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = [UIColor whiteColor];
        _contentLabel.font = kFontSize(17);
    }
    return _contentLabel;
}
- (UIButton *)fistBtn
{
    if (_fistBtn == nil) {
        _fistBtn = [[UIButton alloc] init];
        [_fistBtn setTitle:@"" forState:UIControlStateNormal];
        [_fistBtn setTitleColor:[UIColor color202020] forState:UIControlStateNormal];
        [_fistBtn setBackgroundColor:[UIColor colorFFE300]];
        _fistBtn.layer.masksToBounds = YES;
        _fistBtn.layer.cornerRadius = 40*0.5;
        _fistBtn.titleLabel.font = kFontSize(16);
        [_fistBtn addTarget:self action:@selector(updateButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fistBtn;
}
- (void)updateButtonClick {
}
- (UIButton *)secondBtn
{
    if (_secondBtn == nil) {
        _secondBtn = [[UIButton alloc] init];
        [_secondBtn setBackgroundImage:[UIImage imageNamed:@"updatealert_close"] forState:UIControlStateNormal];
    }
    return _secondBtn;
}
- (UIView *)closeCoverView
{
    if (_closeCoverView == nil) {
        _closeCoverView = [[UIView alloc] init];
        _closeCoverView.backgroundColor = [UIColor clearColor];
        _closeCoverView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        @weakify(self);
        [tap.rac_gestureSignal subscribeNext:^(NSNumber *x) {
            @strongify(self);
            self.hidden = YES;
            [self removeFromSuperview];
        }];
        [_closeCoverView addGestureRecognizer:tap];
    }
    return _closeCoverView;
}
@end
